// pages/ticketlist/ticketlist.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    selectAllStatus:[true,false],
    fapiao:[],
    Invoice_Id:0
  },
  changeinvoiceid:function(e){
    if (this.data.Invoice_Id != 0){
      this.setData({
        Invoice_Id: e.currentTarget.dataset.invoiceid
      })
      app.invoiceid = e.currentTarget.dataset.invoiceid
      app.title = e.currentTarget.dataset.title
      wx.showToast({
        title: '选择成功',
        duration: 2000,
        success: () => {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)

        }
      })
    }
  },
  goToEdit: function(e){
    wx.navigateTo({
      url: '/pages/enterpriseeditor/enterpriseeditor?id=' + e.currentTarget.dataset.id,
    })
  },
  deletefapiao: function (e){
    console.log(e.currentTarget.dataset.id)
    //删除编辑
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': '022',
      'invoice_Id': e.currentTarget.dataset.id,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
      , '').then(
        (res) => {
          wx.showToast({
            title: '删除成功',
            duration: 2000
          })
          var items = this.data.fapiao
          var fapiaos =[]
          for (var i = 0; i < this.data.fapiao.length; i++) {
            var item = this.data.fapiao[i]
            if (item.Id == e.currentTarget.dataset.id) {
               
            }else{
              fapiaos.push(item)
            }
          }
          this.setData({
            fapiao:fapiaos
          })
        },
        (err) => {
          console.log(err.Message, '失败')
        }
      )


  },
  checkboxChange:function(e){
    console.log(e.target.dataset.id)
    var id = e.target.dataset.id
    var items = this.data.fapiao
    for(var i=0 ; i<this.data.fapiao.length;i++)
    {
      var item = this.data.fapiao[i]
      if (item.Id == id){
        if (items[i].Is_Default){
          items[i].Is_Default = false
        }else{
        items[i].Is_Default = true
//设置默认发票
app.invoiceid = id
var memberInfo = wx.getStorageSync('memberInfo')
var postArgs = {
  'Api_Type': '021',
  'invoice_Id': id,
  'Member_Id': memberInfo.MemberId,
  'Member_Token': memberInfo.MemberToken
}
//获取
getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
  , '').then(
    (res) => {
      wx.showToast({
        title: '设置成功',
        duration:2000,
        success:()=>{
          if (this.data.Invoice_Id != 0) {
            setTimeout(()=>{
              wx.navigateBack({ 
                delta: 1
              })
            },2000)
            
          }
        }
      })
     
    },
    (err) => {
      console.log(err.Message, '失败')
    }
  )

        }
      }else{
        items[i].Is_Default = false
      }
    }
    this.setData({
      fapiao: items
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl
    })
    if (options.id){
      this.setData({
        Invoice_Id: options.id
      })
    }
    //检查Session_key是否过期
    wx.checkSession({
      success: (res) => {
        //未过期
        console.log('session_key 发票未过期')
        //是否有数据
        if (true) {
          //没有数据 请求(apitype, pageIndex, classId, keys)
          this.requestPost('018')
        }
      },
      fail: (res) => {
        //过期
        wx.clearStorage({
          success: () => {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
      }
    })
  },
  //发送请求
  requestPost:function(apitype) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
      , '').then(
        (res) => {
          console.log(res, '发票列表')
          if (res.Status == 2) {
            wx.clearStorage({
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          } else if (res.Status == 0) {
            this.setData({
              fapiao: res.Data
            })
          }
         
        },
        (err) => {
          console.log(err.Message, '产品列表信息失败')
        }
      )
  },

  checkAll:function(e)
  {
    var items = this.data.selectAllStatus;
    for(var i=0;i<this.data.selectAllStatus.length;i++)
    {
      if(items[i]==true)
      {
        items[i]=!items[i];
      }
      if (e.detail.value[0]==i)
      {
        items[i]=!this.data.selectAllStatus[i];
      }
    }
    this.setData({
      selectAllStatus: items
    })

  },
  createTicket:function()
  {
    wx.navigateTo({
      url: '/pages/enterpriseeditor/enterpriseeditor',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestPost('018')
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})